<template>
  <!--公司周报管理-->
    <div>
        <public-search
                :fields="fields"
                :search-param="form"
                @on-query="onQuery"
                @on-reset="onReset"
        />
        <public-table
                index
                :page="page"
                :tableData="tableData"
                :tableOption.sync="tableOption"
                @page-change="onPageChange"
        >
            <template #buttons="scope">
                <el-button
                        v-if="scope.row.count!==0"
                        size="small"
                        class="button_edit"
                        type="primary"
                        @click="handle('query',scope.row)"
                >查看</el-button>
                <el-button
                        v-if="scope.row.count===0"
                        size="small"
                        class="button_funtion2"
                        type="primary"
                        @click="handle('add',scope.row)"
                >总结评价</el-button>
                <el-button
                        v-if="scope.row.count!==0"
                        size="small"
                        class="button_view"
                        type="primary"
                        @click="handle('edit',scope.row)"
                >编辑</el-button>
            </template>
        </public-table>
    </div>

  <project-summary-popup
    ref="projectSummaryPopupRef"
    @on-success="onSuccess"
  />
</template>

<script setup>
import PublicTable from "@/components/PublicTable.vue";
import {onMounted, reactive, ref} from "vue";
import {tableCommonFun} from "@/hooks/tableCommon";
import PublicSearch from "@/components/PublicSearch.vue";
import { weeklyApi} from "@/api/investment";
import {department} from "@/api/project";
import {useRouter} from "vue-router";
import {ElButton, ElMessage} from "element-plus";
import ProjectSummaryPopup from "./components/projectSummaryPopup.vue";

const interfaceConfig = reactive({
    fileName:'investment',
    apiPath:'projectSetApi.getPage',
});

const defaultParameters = reactive({
    prepareStatus:1
});

const {
    form,
    page,
    tableOption,
    tableData,
    onQuery,
    onReset,
    onPageChange,
    getData,
    onExport
}  = tableCommonFun(interfaceConfig,{},defaultParameters);

form.value = {
    startTime:'',
    endTime:'',
    title:'',
}

tableOption.value = [
  { label: "项目编号 ", prop: "projectId", minWidth:100 },
  { label: "项目名称", prop: "projectName", minWidth:100 },
  { label: "项目类型", prop: "projectTypeName", minWidth:100 },
  { label: "实施主体", prop: "companyId", minWidth:100 },
  { label: "预计结束时间", prop: "predictEndTime", minWidth:120 },
  {
    label: '操作',
    prop: 'buttons',
    slot: true,
    width: 550,
      fixed: "right"
  }
];

const fields = ref([
    {   type:'input',
        label:'关键字',
        key:'keyword',
        placeholder:'请输入标题',
        clearable:true
    },
    {   type:'datetimerange',
        label:'时间',
        key:'valueTime',
        startTimeName:'startTime',
        endTimeName:'endTime',
        placeholder:'请选择时间',
        clearable:true,
        fun:(e,params)=>{
            if(e && e instanceof Array && e.length>0){
                form.value[params.startTimeName] = e[0];
                form.value[params.endTimeName] = e[1];
            }
            if(!e || (e instanceof  Array && e.length===0)){
                form.value[params.startTimeName] = '';
                form.value[params.endTimeName] = '';
            }
        }
    },
]);

/**
 * 获取所有部门
 */
const getAllDepartments = () => {
    department.getAllDepartments().then((res) => {
        const { code, data, msg } = res;
        if (code == 200) {
            fields.value[3].options.list = data;
        }
    });
};

/**
 * 根据部门id查负责人
 * @param responsibleDepartmentId
 */
const getLeaderByDepartmentId = (responsibleDepartmentId) => {
    department.getLeaderByDepartmentId(responsibleDepartmentId).then((res) => {
        const { code, data, msg } = res;
        if (code == 200) {
            fields.value[4].options.list = data;
        }
    });
};

/**
 * 新增周报
 */
const viewPopoverRef = ref(null);
const addHandle = () => {
    viewPopoverRef.value.open({ type: '建设部' ,isAdd:true})
}

/**
 * 下发周报
 * @param row
 */
const updateWeeklyStatus = (row) => {
    if(row.status===1){
        return ElMessage.warning('已下发不能重复下发!');
    }
    weeklyApi.updateWeeklyStatus({weeklyId:row.weeklyId}).then(res=>{
        if(res.code==='200'){
            ElMessage.success('下发成功!');
            onQuery();
        }
    });
}

/**
 * 周报填写
 * @param row
 */
const router = useRouter();
const toPath = (row) => {
    router.push({path:`/investmentProjectManagement/weeklyReport/weeklyReportFilling/${row.weeklyId}`});
}

/**
 *
 * @param type
 */
const projectSummaryPopupRef = ref(null);
const handle = (type,row) => {
  projectSummaryPopupRef.value.open(row.projectId,type);
}

/**
 * 成功时回调
 */
const onSuccess = () => {
    getData({
        pageNum:1,
        pageSize:10,
        param:{
            prepareStatus:1,
            ...form.value
        }
    });
}

onMounted(()=>{
    getData({
        pageNum:1,
        pageSize:10,
        param:{
            prepareStatus:1
        }
    });
});
</script>

<style scoped>

</style>
